﻿using CPPEI.Coolzon.UI.XProperty.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using RT = CPPEI.Coolzon.Metadata;

namespace CPPEI.Coolzon.UI.XProperty
{
    /// <summary>
    /// XProperty的工具类，用于更新RT.Metadata对象的属性
    /// </summary>
    public static class XPropertyRTUtil
    {
        private static ConvertContext s_convertContext;

        public static void SetConvertContext(ConvertContext convertContext)
        {
            s_convertContext = convertContext;
        }
        /// <summary>
        /// 为场景设置属性的值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rtScene"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue<T>(RT.Scene rtScene, string propertyName, T value)
        {
            CPPEI.Coolzon.UI.XProperty.IPropertyValueConverter converter = CPPEI.Coolzon.UI.XProperty.PropertyValueConverterFactory.GetConverter(typeof(T));
            if (converter != null)
            {
                CPPEI.Coolzon.Metadata.Property property = CPPEI.Coolzon.Metadata.SolutionUtil.GetProperty(rtScene.Properties, propertyName);
                if (property != null)
                {
                    property.ValueString = string.Empty;
                    property.ValueNode = converter.ConvertBack(s_convertContext, value);
                }
                else
                {
                    property = new Coolzon.Metadata.Property();
                    property.Name = propertyName;
                    property.ValueNode = converter.ConvertBack(s_convertContext, value);
                    rtScene.Properties.Add(property);
                }
            }
        }

        /// <summary>
        /// 为区域设置属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rtArea"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue<T>(RT.Area rtArea, string propertyName, T value)
        {
            CPPEI.Coolzon.UI.XProperty.IPropertyValueConverter converter = CPPEI.Coolzon.UI.XProperty.PropertyValueConverterFactory.GetConverter(typeof(T));
            if (converter != null)
            {
                CPPEI.Coolzon.Metadata.Property property = CPPEI.Coolzon.Metadata.SolutionUtil.GetProperty(rtArea.Properties, propertyName);
                if (property != null)
                {
                    property.ValueString = string.Empty;
                    property.ValueNode = converter.ConvertBack(s_convertContext, value);
                }
                else
                {
                    property = new Coolzon.Metadata.Property();
                    property.Name = propertyName;
                    property.ValueNode = converter.ConvertBack(s_convertContext, value);
                    rtArea.Properties.Add(property);
                }
            }
        }

        /// <summary>
        /// 为组件设置属性值
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rtPart"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue<T>(RT.Part rtPart, string propertyName, T value)
        {
            CPPEI.Coolzon.UI.XProperty.IPropertyValueConverter converter = CPPEI.Coolzon.UI.XProperty.PropertyValueConverterFactory.GetConverter(typeof(T));
            if (converter != null)
            {
                CPPEI.Coolzon.Metadata.Property property = CPPEI.Coolzon.Metadata.SolutionUtil.GetProperty(rtPart.Properties, propertyName);
                if (property != null)
                {
                    property.ValueString = string.Empty;
                    property.ValueNode = converter.ConvertBack(s_convertContext, value);
                }
                else
                {
                    property = new Coolzon.Metadata.Property();
                    property.Name = propertyName;
                    property.ValueNode = converter.ConvertBack(s_convertContext, value);
                    rtPart.Properties.Add(property);
                }
            }
        }

        /// <summary>
        /// 为原数据设置属性值。
        /// 通过值的类型获取值转换器，然后通过转换器的反向转换得到值的配置节点对象。
        /// 将配置节点信息更新到原数据的property中。
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="rtNode"></param>
        /// <param name="propertyName"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue<T>(RT.NodeBase rtNode, string propertyName, T value)
        {
            CPPEI.Coolzon.UI.XProperty.IPropertyValueConverter converter = CPPEI.Coolzon.UI.XProperty.PropertyValueConverterFactory.GetConverter(typeof(T));
            if (converter != null)
            {
                CPPEI.Coolzon.Metadata.Property property = CPPEI.Coolzon.Metadata.SolutionUtil.GetProperty(rtNode.Properties, propertyName);
                if (property != null)
                {
                    property.ValueString = string.Empty;
                    property.ValueNode = converter.ConvertBack(s_convertContext, value);
                }
                else
                {
                    property = new Coolzon.Metadata.Property();
                    property.Name = propertyName;
                    property.ValueNode = converter.ConvertBack(s_convertContext, value);
                    rtNode.Properties.Add(property);
                }
            }
        }

        /// <summary>
        /// 更新属性值
        /// </summary>
        /// <param name="rtNode"></param>
        /// <param name="propertyName"></param>
        /// <param name="valueType"></param>
        /// <param name="value"></param>
        public static void SetPropertyValue(RT.NodeBase rtNode, string propertyName, Type valueType, object value)
        {
            CPPEI.Coolzon.UI.XProperty.IPropertyValueConverter converter = CPPEI.Coolzon.UI.XProperty.PropertyValueConverterFactory.GetConverter(valueType);
            if (converter != null)
            {
                CPPEI.Coolzon.Metadata.Property property = CPPEI.Coolzon.Metadata.SolutionUtil.GetProperty(rtNode.Properties, propertyName);
                if (property != null)
                {
                    if (value != null)
                    {
                        property.ValueString = string.Empty;
                        property.ValueNode = converter.ConvertBack(s_convertContext, value);
                    }
                    else
                    {
                        rtNode.Properties.Remove(property);
                    }
                }
                else
                {
                    if (value != null)
                    {
                        property = new Coolzon.Metadata.Property();
                        property.Name = propertyName;
                        property.ValueNode = converter.ConvertBack(s_convertContext, value);
                        rtNode.Properties.Add(property);
                    }
                }
            }
        }
    }
}
